package com.ruoyi.yipaiju.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.enums.dict.ForeclosedHouseStatus;
import com.ruoyi.common.enums.dict.SpiderHouseGrabStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.ServerManage;
import com.ruoyi.yipaiju.service.IServerManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.yipaiju.mapper.SpiderHouseMapper;
import com.ruoyi.yipaiju.domain.SpiderHouse;
import com.ruoyi.yipaiju.service.ISpiderHouseService;

/**
 * 爬虫房源列Service业务层处理
 * 
 * @author liuwp
 * @date 2023-06-23
 */
@Service
public class SpiderHouseServiceImpl implements ISpiderHouseService 
{
    @Autowired
    private SpiderHouseMapper spiderHouseMapper;
    @Autowired
    private IServerManageService serverManageService;

    /**
     * 查询爬虫房源列
     * 
     * @param id 爬虫房源列主键
     * @return 爬虫房源列
     */
    @Override
    public SpiderHouse selectSpiderHouseById(Long id)
    {
        return spiderHouseMapper.selectSpiderHouseById(id);
    }

    /**
     * 查询爬虫房源列列表
     * 
     * @param spiderHouse 爬虫房源列
     * @return 爬虫房源列
     */
    @Override
    public List<SpiderHouse> selectSpiderHouseList(SpiderHouse spiderHouse)
    {
        return spiderHouseMapper.selectSpiderHouseList(spiderHouse);
    }

    /**
     * 根据业务主键查询一条记录：爬虫房源列
     *
     * @param spiderHouse 爬虫房源列
     * @return 爬虫房源列
     */
    @Override
    public SpiderHouse selectOneSpiderHouse(SpiderHouse spiderHouse)
    {
        return spiderHouseMapper.selectOneSpiderHouse(spiderHouse);
    }

    /**
     * 新增爬虫房源列
     * 
     * @param spiderHouse 爬虫房源列
     * @return 结果
     */
    @Override
    public int insertSpiderHouse(SpiderHouse spiderHouse)
    {
        spiderHouse.setCreateTime(DateUtils.getNowDate());
        return spiderHouseMapper.insertSpiderHouse(spiderHouse);
    }

    /**
     * 修改爬虫房源列
     * 
     * @param spiderHouse 爬虫房源列
     * @return 结果
     */
    @Override
    public int updateSpiderHouse(SpiderHouse spiderHouse)
    {
        spiderHouse.setUpdateTime(DateUtils.getNowDate());
        return spiderHouseMapper.updateSpiderHouse(spiderHouse);
    }

    @Override
    public int updateSpiderHouseCondition(SpiderHouse spiderHouse, SpiderHouse spiderHouseCondition)
    {
                spiderHouse.setUpdateTime(DateUtils.getNowDate());
        return spiderHouseMapper.updateSpiderHouseCondition(spiderHouse, spiderHouseCondition);
    }

    /**
     * 批量删除爬虫房源列
     * 
     * @param ids 需要删除的爬虫房源列主键
     * @return 结果
     */
    @Override
    public int deleteSpiderHouseByIds(Long[] ids)
    {
        return spiderHouseMapper.deleteSpiderHouseByIds(ids);
    }

    /**
     * 删除爬虫房源列信息
     * 
     * @param id 爬虫房源列主键
     * @return 结果
     */
    @Override
    public int deleteSpiderHouseById(Long id)
    {
        return spiderHouseMapper.deleteSpiderHouseById(id);
    }

    /**
     * 条件删除爬虫房源列
     *
     * @param spiderHouse 爬虫房源列
     * @return 结果
     */
    @Override
    public int deleteSpiderHouseByCondition(SpiderHouse spiderHouse)
    {
        return spiderHouseMapper.deleteSpiderHouseByCondition(spiderHouse);
    }

    @Override
    public ApiResult spiderHouseListHandle(ServerManage serverManage, String serverIp) {
        ServerManage serverManage1 = serverManageService.selectOneServerManage(Builder.of(ServerManage::new)
                .with(ServerManage::setServerIp, serverIp)
                .build());
        if (serverManage1 != null){
            serverManage1.setGrabCity(serverManage.getGrabCity());
            serverManage1.setGrabDistrict(serverManage.getGrabDistrict());
            serverManage1.setHouseType(serverManage.getHouseType());
            serverManage1.setGrabPage(serverManage.getGrabPage());
            serverManageService.updateServerManage(serverManage1);
        }

        // TODO 这里按标题过滤下数据
        Iterator<SpiderHouse> iterator = serverManage.getSpiderHouses().iterator();
        while (iterator.hasNext()){
            SpiderHouse next = iterator.next();
            if (StringUtils.isNotEmpty(next.getTitle())){
                if (next.getTitle().contains("商标") ||
                        next.getTitle().contains("专利") ||
                        next.getTitle().contains("软著") ||
                        next.getTitle().contains("汽车")){
                    iterator.remove();
                }
            }
        }

        List<SpiderHouse> spiderHouses1 = selectSpiderHouseListByCodes(serverManage.getSpiderHouses());
        // 数据库中的转map
        Map<String, SpiderHouse> spiderHouse1Map = spiderHouses1.stream().collect(Collectors.toMap(SpiderHouse::getHouseCode, item -> item, (value1, value2) -> value2));

        // 拿到需要插入的
        List<SpiderHouse> insertList = new ArrayList<>();
        List<SpiderHouse> updateList = new ArrayList<>();

        for (SpiderHouse spiderHouse : serverManage.getSpiderHouses()) {
            SpiderHouse spiderHouse1 = spiderHouse1Map.get(spiderHouse.getHouseCode());
            if (spiderHouse1 == null){
                // 插入的
                spiderHouse.setServerIp(serverIp);
                spiderHouse.setCreateTime(new Date());
                spiderHouse.setGrabStatus(SpiderHouseGrabStatus.WAITGRAB.getValue());
                spiderHouse.setPageNum(serverManage.getGrabPage());
                insertList.add(spiderHouse);
            }else {
                // 修改的
                // 判断状态是否有变动，如果有变动则重新爬取数据   TODO 如果是进行中的则无条件爬取
//                TODO("预告中", "预告中"),
//                DOING("进行中", "进行中"),
                if (ForeclosedHouseStatus.TODO.getValue().equalsIgnoreCase(spiderHouse.getStatus())
                        || ForeclosedHouseStatus.DOING.getValue().equalsIgnoreCase(spiderHouse.getStatus())
                        || !spiderHouse.getStatus().equals(spiderHouse1.getStatus())){
                    spiderHouse1.setGrabStatus(SpiderHouseGrabStatus.WAITGRAB.getValue());
                    spiderHouse1.setStatus(spiderHouse.getStatus());
                }
                spiderHouse1.setPageNum(serverManage.getGrabPage());
                spiderHouse1.setUpdateTime(new Date());

                spiderHouse1.setOrigin(spiderHouse.getOrigin());
                spiderHouse1.setAuctionUrl(spiderHouse.getAuctionUrl());
                spiderHouse1.setAssessmentPrice(spiderHouse.getAssessmentPrice());
                spiderHouse1.setCurrentPrice(spiderHouse.getCurrentPrice());
                spiderHouse1.setDiscountRate(spiderHouse.getDiscountRate());
                spiderHouse1.setEnsurePrice(spiderHouse.getEnsurePrice());
                spiderHouse1.setMarketPrice(spiderHouse.getMarketPrice());
                spiderHouse1.setMinPrice(spiderHouse.getMinPrice());
                spiderHouse1.setAddress(spiderHouse.getAddress());
                spiderHouse1.setCourt(spiderHouse.getCourt());
                spiderHouse1.setTitle(spiderHouse.getTitle());
                spiderHouse1.setStartTime(spiderHouse.getStartTime());
                spiderHouse1.setEndTime(spiderHouse.getEndTime());
                spiderHouse1.setExt1(spiderHouse.getExt1());
                spiderHouse1.setExt2(spiderHouse.getExt2());
                spiderHouse1.setExt3(spiderHouse.getExt3());

                updateList.add(spiderHouse1);
            }
        }

        if (insertList.size() > 0){
            insertSpiderHouseList(insertList);
        }
        if (updateList.size() > 0){
            // TODO 修改京东的价格等数据
            updateSpiderHouseList(updateList);
        }
        return ApiResult.success();
    }

    @Override
    public List<SpiderHouse> selectSpiderHouseListByCodes(List<SpiderHouse> spiderHouses) {
        return spiderHouseMapper.selectSpiderHouseListByCodes(spiderHouses);
    }

    @Override
    public int insertSpiderHouseList(List<SpiderHouse> spiderHouses) {
        return spiderHouseMapper.insertSpiderHouseList(spiderHouses);
    }

    @Override
    public int updateSpiderHouseList(List<SpiderHouse> spiderHouses) {
        return spiderHouseMapper.updateSpiderHouseList(spiderHouses);
    }

    @Override
    public int updateSpiderHouseListGrabing(SpiderHouse spiderHouse) {
        return spiderHouseMapper.updateSpiderHouseListGrabing(spiderHouse);
    }
}
